package com.tenebrous.jestic.service.framwork.meber.organ;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tenebrous.jestic.entity.framwork.meber.organ.OrgUser;
import com.tenebrous.jestic.entity.framwork.meber.organ.OrgUserTrain;
import com.tenebrous.jestic.mapper.idao.framwork.meber.organ.OrgUserTrainMapper;
import com.tenebrous.jestic.service.SuperService;
import com.rain.ops.sdk.dto.ApiResult;
import com.rain.ops.sdk.utils.UUIDUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class OrgUserTrainService extends SuperService<OrgUserTrain> {

    @Autowired
    private OrgUserTrainMapper mapper;

    @Override
    @Autowired
    protected void setSuperMapper() {
        super.setSuperMapper(this.mapper);
    }

    public List<OrgUserTrain> getTrainList(String userid) {
        return this.mapper.selectTrainListByUserid(userid);
    }

    public PageInfo queryPagingList(Integer pageIndex, Integer pageSize, String userid) {
        return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> this.mapper.selectTrainListByUserid(userid));
    }

    public PageInfo<OrgUserTrain> queryPagingListCon(Integer pageIndex, Integer pageSize, String title, String userid) {
        return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> this.mapper.selectByConditionP(title, userid));
    }

    @Transactional
    public int deleteByIds(String[] ids) {
        int i = 0;
        for (String id : ids) {
            if (StringUtils.isNotBlank(id)) {
                i += this.mapper.deleteByPrimaryKey(id);
            }
        }
        return i;
    }

    @Transactional
    public ApiResult saveOrUpdateTrain(String trainStr, String uid, OrgUser user) {
        if (StringUtils.isNotBlank(trainStr) && StringUtils.isNotBlank(uid)) {
            List<OrgUserTrain> outList = this.getTrainList(uid);
            if (outList.size() > 0) {//更新
                this.mapper.deleteByUid(uid);
            }
            List<OrgUserTrain> list = JSON.parseArray(trainStr, OrgUserTrain.class);
            int i = 0;
            for (OrgUserTrain item : list) {
                i = i + 1;
                if (StringUtils.isBlank(item.getId())) {
                    item.setId(UUIDUtils.randomUUID());
                }
                item.setSort(i);
                item.setUserid(uid);
                item.setCreateDate(new Date());
                item.setCreateUserid(user.getId());
                item.setUpdateDate(new Date());
                item.setUpdateUserid(user.getId());
                String sedatestr = item.getSedate();
                if (StringUtils.isNotBlank(sedatestr)) {
                    List<Date> array = JSON.parseArray(sedatestr, Date.class);
                    System.out.println(array);
                }
                this.mapper.insertSelective(item);
            }
            return new ApiResult(200, "保存成功。");
        }
        return new ApiResult(999, "保存失败。");

    }

    public List<OrgUserTrain> selectMax(String userid) {
        return this.mapper.selectMax(userid);
    }
}
